<?php
namespace Admin\Controller;

use Think\Controller;

class RemindersController extends RightController
{
    // 加载初始化内容页,并检索
    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_reminders");
            $this->assign("params", session("QG_SEARCH_PARAMS_reminders"));
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_reminders");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_reminders", null);
            session("QG_SEARCH_PARAMS_reminders", null);
            session("QG_SEARCH_SIZE_reminders", null);
            $pageSize = I("post.pageSize");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }
		
		// 分页输入
        if(empty($pageSize)){
            $pageSize = 10;
        }

        // 总条数
        $count = M('myreminders')
            ->alias("t")
            ->field("t.*,m.realName")
            ->join("LEFT JOIN t_member m ON m.id=t.memberId")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('myreminders')
            ->alias("t")
            ->field("t.*,m.realName,m.account")
            ->join("LEFT JOIN t_member m ON m.id=t.memberId")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();

        // 渲染查询条件到session
        session("QG_SEARCH_WHERE_reminders", $where);
		session("QG_SEARCH_PARAMS_reminders", I("post."));
		session("QG_SEARCH_SIZE_reminders", $pageSize);
		
		// 输出数据
        $this->assign("params", I("post."));
        $this->assign('list', $list);
		$this->assign('lists', array(
            "page" => getDropdownList("pageSize" , '' , false),
        ));
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";

        // 账号
        if (I('post.account')) {
            // 精确
            if (I('post.accountCondition') == 1) {
                $where .= " AND m.account='" . I("post.account") . "'";
            } else {
                $where .= " AND m.account LIKE '%" . I("post.account") . "%'";
            }
        }

        // 姓名
        if (I('post.name')) {
            // 精确
            if (I('post.nameCondition') == 1) {
                $where .= " AND m.realName='" . I("post.name") . "'";
            } else {
                $where .= " AND m.realName LIKE '%" . I("post.name") . "%'";
            }
        }

        // 还款时间
        if (I('post.remindersDate')) {
            $where .= " AND t.remindersDate='" . I("post.remindersDate") . "'";
        }

        // 创建时间
        if (I('post.createtimeStart') || I('post.createtimeEnd')) {
            // 起始日期
            if(I('post.createtimeStart')){
                $where .= " AND t.createTime>='" . I("post.createtimeStart") . "'";
            }
            // 结束日期
            if(I('post.createtimeEnd')){
                $where .= " AND t.createTime<='" . I("post.createtimeEnd") . " 23:59:59'";
            }
        }

        return $where;
    }

    // 添加
    public function add()
    {
        if (IS_POST) {
            // 数据
            $data = array();

            // 账号
            $account = trim(I('post.account'));
            $userInfo = M('member')->where(array("account"=>$account))->find();
            if (empty($userInfo)) $this->ajaxError('账号不存在！');

            $data['memberId'] = $userInfo['id'];

            //状态
            $data['state'] = (int)I('post.status');

            //金额
            $data['money'] = I('post.money');

            //还款日期
            $data['remindersDate'] = I('post.remindersDate');

            // 还款 凭证
            $data['evidence'] = I('post.evidence');

            // 缩略图
            $data['evidenceImg'] = I('post.evidenceImg');

            // 创建时间
            $data['createTime'] = now_datetime();

            //创建数据
            $result = M('myreminders')->add($data);

            if ($result) {
				$this->ajaxSuccess("添加成功！");
            } else {
				$this->ajaxError("添加失败！");
            }
        } else {
            $this->assign("lists", array(
                "reminderStatus" => getDropdownList("reminderStatus", '', false),
            ));
            $this->display();
        }
    }

    // 编辑资料
    public function edit()
    {
        if (IS_POST) {
            // ID
            $id = I("post.id");
			
			// 没有ID
			if(empty($id)){
				$this->ajaxError("错误！");
			}

            // 判断是否存在
            $dbData = M('myreminders')
                ->where(array("id", $id))
                ->find();

            //判断用户是否存在
            if (empty($dbData)) {
				$this->ajaxError("数据不存在！");
            }

            // 更新数据
            $data = array();

            // 账号
            $account = trim(I('post.account'));
            $userInfo = M('member')->where(array("account"=>$account))->find();
            if (empty($userInfo)) $this->ajaxError('账号不存在！');

            $data['memberId'] = $userInfo['id'];

            //状态
            $data['state'] = (int)I('post.reminderStatus');

            //金额
            $data['money'] = I('post.money');

            //还款日期
            $data['remindersDate'] = I('post.remindersdate');

            // 还款 凭证
            $data['evidence'] = I('post.evidence');

            // 缩略图
            $data['evidenceImg'] = I('post.evidenceimg');

            // 更新数据
            $result = M('myreminders')
                ->where(array("id" => $id))
                ->save($data);

            if ($result !== false) {
				$this->ajaxSuccess("更新成功！");
            } else {
				$this->ajaxError("更新失败！");
            }
        } else {
            // ID
            $id = intval(I("get.id"));

            // 判断是否存在
            $data = M('myreminders')
                ->alias("t")
                ->field("t.*,m.realName,m.account")
                ->join("LEFT JOIN t_member m ON m.id=t.memberId")
                ->where(array("t.id" => $id))
                ->find();

            // 判断是否存在
            if (empty($data)) {
                $this->error("数据不存在");
            }

            $this->assign("lists", array(
                "reminderStatus" => getDropdownList("reminderStatus", '', false),
            ));

            $this->assign("data", $data);
            $this->display();
        }
    }


    // 删除
    public function delete()
    {
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));
			
			// 没有ID
			if(empty($id)){
				$this->ajaxError("错误！");
			}

            // 更新数据
            $result = M('share')->where(array("id" => $id))->delete();

            // 如果成功
            if ($result) {
				$this->ajaxSuccess("删除成功！");
            } else {
				$this->ajaxError("删除失败！");
            }
        }
    }
}